/**
 * 报表设计公司(2011)
 */
package net.abbp.report.model.cell.style.background;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.JViewport;
import javax.swing.Scrollable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.reportgear.report.view.grid.util.GraphHelper;

/**
 * @version 1.0 2011-3-25
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class ImagePreviewPane extends JComponent implements Scrollable {

	private Image image = null;
	private int width = 0;
	private int height = 0;
	private List changeListenerList = new ArrayList();

	public ImagePreviewPane() {
		setToolTipText("View Image");
	}

	public Image getImage() {
		return this.image;
	}

	public void setImage(Image paramImage) {
		this.image = paramImage;
		if (this.image == null) {
			if (getParent() instanceof JViewport) {
				JScrollBar localJScrollBar = new JScrollBar(0);
				Dimension localDimension = new Dimension(getSize().width - localJScrollBar.getPreferredSize().height,
						getSize().height - localJScrollBar.getPreferredSize().height);
				((JViewport) getParent()).setPreferredSize(localDimension);
				getParent().setSize(localDimension);
				((JViewport) getParent()).setMinimumSize(localDimension);
				((JViewport) getParent()).setMaximumSize(localDimension);
			}
		} else {
			GraphHelper.waitForImage(paramImage);
			this.width = paramImage.getWidth(null);
			this.height = paramImage.getHeight(null);
			setToolTipText("Image Size: " + this.width + "x" + this.height + "px");
			fireChagneListener();
		}
		revalidate();
	}

	public void paintComponent(Graphics paramGraphics) {
		super.paintComponent(paramGraphics);
		if (getImage() == null) {
			return;
		}
		paramGraphics.drawImage(getImage(), 0, 0, this);
	}

	public Dimension getPreferredSize() {
		if (this.image == null) {
			return super.getPreferredSize();
		}
		return new Dimension(this.width, this.height);
	}

	public void addChangeListener(ChangeListener paramChangeListener) {
		this.changeListenerList.add(paramChangeListener);
	}

	private void fireChagneListener() {
		if (this.changeListenerList.size() <= 0) {
			return;
		}
		ChangeEvent localChangeEvent = new ChangeEvent(getImage());
		for (int i = 0; i < this.changeListenerList.size(); ++i) {
			((ChangeListener) this.changeListenerList.get(i)).stateChanged(localChangeEvent);
		}
	}

	public Dimension getPreferredScrollableViewportSize() {
		return getPreferredSize();
	}

	public int getScrollableUnitIncrement(Rectangle paramRectangle, int paramInt1, int paramInt2) {
		switch (paramInt1) {
		case 1:
			return (paramRectangle.height / 10);
		case 0:
			return (paramRectangle.width / 10);
		}
		throw new IllegalArgumentException("Invalid orientation: " + paramInt1);
	}

	public int getScrollableBlockIncrement(Rectangle paramRectangle, int paramInt1, int paramInt2) {
		switch (paramInt1) {
		case 1:
			return paramRectangle.height;
		case 0:
			return paramRectangle.width;
		}
		throw new IllegalArgumentException("Invalid orientation: " + paramInt1);
	}

	public boolean getScrollableTracksViewportWidth() {
		if (getParent() instanceof JViewport) {
			return (getParent().getWidth() > getPreferredSize().width);
		}
		return false;
	}

	public boolean getScrollableTracksViewportHeight() {
		if (getParent() instanceof JViewport) {
			return (getParent().getHeight() > getPreferredSize().height);
		}
		return false;
	}

}
